package io.github.wslxm.springbootplus2.manage.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import io.github.wslxm.springbootplus2.core.base.service.impl.BaseServiceImpl;
import io.github.wslxm.springbootplus2.core.utils.XjBeanUtil;
import io.github.wslxm.springbootplus2.core.utils.tree.XjTreeUtil;
import io.github.wslxm.springbootplus2.manage.sys.mapper.SysAddressMapper;
import io.github.wslxm.springbootplus2.manage.sys.service.SysAddressService;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysAddress;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysAddressVO;
import io.github.wslxm.springbootplus2.manage.sys.model.dto.SysAddressDTO;
import io.github.wslxm.springbootplus2.manage.sys.model.query.SysAddressQuery;
import org.apache.commons.lang3.StringUtils;
import java.util.List;

/**
 *  ServiceImpl
 *
 * <p>
 * ::本代码由[兮家小二]提供的代码生成器生成,如有问题,请手动修改 ::作者CSDN:https://blog.csdn.net/qq_41463655
 * </p>

 * @author ws
 * @email 1720696548@qq.com
 * @date 2023-03-24 10:04:20
 */
@Service
public class SysAddressServiceImpl extends BaseServiceImpl<SysAddressMapper, SysAddress> implements SysAddressService {


    @Override
    public List<SysAddressVO> tree(SysAddressQuery query) {
        LambdaQueryWrapper<SysAddress> queryWrapper = new LambdaQueryWrapper<SysAddress>().orderByDesc(SysAddress::getCreateTime);
        queryWrapper.in(SysAddress::getLevel, CollUtil.newArrayList(0,1));
        List<SysAddress> list = this.list(queryWrapper);
        List<SysAddressVO> listVo = XjBeanUtil.listVo(list, SysAddressVO.class);
        return XjTreeUtil.nextTree(listVo, "0").getChildren();
    }


    @Override
    public BasePage<SysAddressVO> findPage(SysAddressQuery query) {
        LambdaQueryWrapper<SysAddress> queryWrapper = new LambdaQueryWrapper<SysAddress>().orderByDesc(SysAddress::getCreateTime);
        queryWrapper.eq(StringUtils.isNotBlank(query.getPid()), SysAddress::getPid, query.getPid());
        queryWrapper.likeRight(StringUtils.isNotBlank(query.getName()), SysAddress::getName, query.getName());
        queryWrapper.eq(StringUtils.isNotBlank(query.getCode()), SysAddress::getCode, query.getCode());
        queryWrapper.eq(query.getLevel() != null, SysAddress::getLevel, query.getLevel());
        Page<SysAddress> page = this.page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);
        return XjBeanUtil.pageVo(page, SysAddressVO.class);
    }

    @Override
    public SysAddressVO findId(String id) {
        return XjBeanUtil.convert(this.getById(id), SysAddressVO.class);
    }

    @Override
    public String insert(SysAddressDTO dto) {
        SysAddress entity = dto.convert(SysAddress.class);
        boolean b = this.save(entity);
        return entity.getId();
    }

    @Override
    public boolean upd(String id, SysAddressDTO dto) {
        SysAddress entity = dto.convert(SysAddress.class);
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public boolean del(String id) {
         List<SysAddress> list = this.list(new LambdaQueryWrapper<SysAddress>().select(SysAddress::getId, SysAddress::getPid));
         List<SysAddressVO> listVos = XjBeanUtil.listVo(list, SysAddressVO.class);
         List<String> nextIds = XjTreeUtil.getNextIds(listVos, id);
         nextIds.add(id);
         return this.removeBatchByIds(nextIds);
    }

}
